package io.renren.modules.app.message.handler;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import io.renren.modules.app.message.MessageResult;
import io.renren.modules.app.message.MsgType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author :
 * @version V1.0
 * @Project:
 * @Package
 * @Description: 商品消息处理器
 * @date
 **/
@Component(ProductPriceMessageHandler.BeanName)
@Slf4j
public class ProductPriceMessageHandler extends AbstractJdVopMessageHandler {

//    @Autowired
//    private JdVopProductSoldOutHandler jdVopProductSoldOutHandler;
//
//    @Autowired
//    private PmsProductValidateDataServiceImpl pmsProductValidateDataService;

    public static final String BeanName = "MessageProductPriceHandle";

    private ThreadPoolExecutor productExecutor= new ThreadPoolExecutor(2,10, 20, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>(500));
    /**
     * 商品价格变动消息处理总条数
     */
    private static Integer COUNT=0;
    /**
     * 商品价格变动消息处理条数
     */
    private static Integer PRICE=0;

    public ProductPriceMessageHandler(ApplicationContext applicationContext) {
        super(applicationContext);
        setBeanName(BeanName);
        productExecutor.allowCoreThreadTimeOut(true);
    }


    @Override
    public void read(MessageResult msg) {
        ++COUNT;
        log.info("商品价格变动消息处理总条数：{}，时间：{}",COUNT,new Date());
//        int type = msg.getType();
//        switch (type) {
//            case MsgType.Product.PRICE:
//                productExecutor.execute(() -> this.handleProductPrice(msg));
//                break;
//            default:
//                log.info("JD_VOP NOT FOUND FUN：{}", msg);
//                notifyMsgReadEvent(msg, false);
//        }
    }

    /**
     * 更新商品价格并推送删除消息
     *
     * @param msg
     */
    public void handleProductPrice(MessageResult msg) {
        ++PRICE;
        log.info("商品价格变动消息处理条数：{}，时间：{}",PRICE,new Date());
////        log.info("1.msg={}",msg);
//        String skuId = getMsg(msg, MessageResult.skuId);
//        Date date=msg.getTime();
//        //验证是否已经记录
//        PmsProductValidateData validateData=this.pmsProductValidateDataService.findOneBySkuId(skuId);
//        //如果已经存在记录，且价格修改时间不为空且>=推送时间,则直接删除掉
//        if(ObjectUtil.isNotNull(validateData)
//                && ObjectUtil.isNotNull(validateData.getPriceTime())
//                && DateUtil.compare(validateData.getPriceTime(),date)>=0){
////            log.info("2.已存在记录: {}>={}",DateUtil.formatDateTime(validateData.getPriceTime()),DateUtil.formatDateTime(msg.getTime()));
//            notifyMsgReadEvent(msg, true);
//            return;
//        }
//        boolean flag=jdVopProductSoldOutHandler.dealProductPriceMessageBySkuId(skuId,date);
//        notifyMsgReadEvent(msg,flag);
    }




    @Override
    public void batchRead(List<MessageResult> msgs) {
        System.out.println("商品价格变动处理");
        msgs.parallelStream().forEach(this::read);
    }

}
